using System.Collections.Generic;
using UnityEngine;

public class Dog : MonoBehaviour
{
	public enum State
	{
		Idle = 0,
		Follow = 1,
		Woof = 2,
		Attack = 3
	}

	public Animator animator;

	public AudioSource audioSource;

	public Collider2D detectCollider;

	public float actionInterval;

	public Vector3 movePos;

	public AudioClip[] woof;

	public AudioClip[] attackSounds;

	public bool canAttack;

	[Header("Runtime")]
	public Player player;

	public float defaultScaleX;

	public bool isAttacking;

	public float actionCooldown;

	public State state;

	public float move;

	public bool movePosReached;

	public List<Zombie> detectedZombies = new List<Zombie>();

	public Zombie targetZombie;

	private void Start()
	{
		canAttack = true;
		player = GameObject.Find("Player").GetComponent<Player>();
		defaultScaleX = base.transform.localScale.x;
		Invoke("GoToPlayer", 0.1f);
	}

	public void GoToPlayer()
	{
		base.transform.position = CampaignGame.Instance.player.transform.position;
	}

	private void Update()
	{
		actionCooldown -= Time.deltaTime;
		for (int i = 0; i < detectedZombies.Count; i++)
		{
			if (!detectedZombies[i])
			{
				detectedZombies.RemoveAt(i);
			}
			else if (detectedZombies[i].body.isDead || !detectedZombies[i].body)
			{
				detectedZombies.RemoveAt(i);
			}
		}
		animator.SetInteger("ZombieCount", detectedZombies.Count);
		if (!targetZombie && detectedZombies.Count > 0)
		{
			float num = 999f;
			for (int j = 0; j < detectedZombies.Count; j++)
			{
				if (IsValidTarget(detectedZombies[j]) && detectedZombies[j].playerDistance < num)
				{
					targetZombie = detectedZombies[j];
					num = detectedZombies[j].playerDistance;
				}
			}
		}
		if (!IsValidTarget(targetZombie))
		{
			targetZombie = null;
		}
		if (canAttack && !player.isDead && player.zombieContactCount > 0)
		{
			for (int k = 0; k < detectedZombies.Count; k++)
			{
				if (detectedZombies[k].isTouchPlayer)
				{
					targetZombie = detectedZombies[k];
					break;
				}
			}
			actionCooldown -= Random.value * actionInterval;
			state = State.Attack;
		}
		else if (actionCooldown <= 0f)
		{
			animator.SetBool("Sit", value: false);
			if ((bool)targetZombie && canAttack)
			{
				state = State.Attack;
			}
			else if (detectedZombies.Count <= 0 && Vector2.Distance(player.transform.position, base.transform.position) > 5f)
			{
				Follow(1f);
			}
			else if (detectedZombies.Count <= 0 && Random.value > 0.5f)
			{
				Follow(Random.Range(0.2f, 0.5f));
			}
			else
			{
				state = State.Idle;
				animator.SetBool("Sit", value: true);
			}
			actionCooldown = Random.value * actionInterval;
		}
	}

	private void FixedUpdate()
	{
		animator.SetFloat("Move", 0f);
		if ((bool)targetZombie && state == State.Attack)
		{
			if (!isAttacking)
			{
				SetMovePos(targetZombie.transform.position);
				move = 1f;
			}
			if ((bool)targetZombie && !isAttacking && Mathf.Abs(base.transform.position.x - targetZombie.transform.position.x) < 1f && Mathf.Abs(base.transform.position.y - targetZombie.transform.position.y) < 0.2f)
			{
				isAttacking = true;
				animator.SetTrigger("JumpAttack");
			}
		}
		else if (state != State.Follow && state == State.Idle)
		{
			movePosReached = true;
		}
		Move();
		if (detectedZombies.Count > 0 && movePosReached && (bool)detectedZombies[0])
		{
			FlipByPosition(detectedZombies[0].transform.position);
		}
	}

	public bool IsValidTarget(Zombie zombie)
	{
		if (!zombie || !zombie.body || !zombie.isWeaponKnockbackable || zombie.gameObject.layer != 9 || (bool)zombie.animalTrap)
		{
			return false;
		}
		return true;
	}

	public void SetMovePos(Vector3 position)
	{
		movePos = position;
		movePosReached = false;
	}

	public void Move()
	{
		if (!movePosReached && !isAttacking && !(Vector2.Distance(base.transform.position, movePos) < 0.1f))
		{
			if (Vector2.Distance(movePos, base.transform.position) < 0.1f)
			{
				movePosReached = true;
			}
			movePos.y = Mathf.Min(-0.1f, Mathf.Max(-2.5f, movePos.y));
			Vector3 normalized = (base.transform.position - movePos).normalized;
			normalized.y *= 0.65f;
			base.transform.position -= normalized * 0.15f * move;
			animator.SetFloat("Move", move);
			FlipByPosition(movePos);
		}
	}

	public void FlipByPosition(Vector3 position)
	{
		Vector3 localScale = base.transform.localScale;
		if (position.x > base.transform.position.x)
		{
			localScale.x = defaultScaleX;
			base.transform.localScale = localScale;
		}
		else
		{
			localScale.x = 0f - defaultScaleX;
			base.transform.localScale = localScale;
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.tag == "Zombie")
		{
			Zombie component = collision.GetComponent<Zombie>();
			if (detectCollider.IsTouching(collision) && !detectedZombies.Contains(component))
			{
				detectedZombies.Add(component);
				actionCooldown = 0f;
			}
		}
	}

	public void AttackStart()
	{
		actionCooldown = 5f;
		isAttacking = true;
	}

	public void AttackHit()
	{
		if (!targetZombie || !targetZombie.animator || (bool)targetZombie.animalTrap)
		{
			return;
		}
		if (targetZombie.isTouchPlayer)
		{
			if (targetZombie.transform.position.x < player.transform.position.x)
			{
				targetZombie.Knockback(2000f, "fallDownFront");
			}
			else
			{
				targetZombie.Knockback(2000f, "fallDownBack");
			}
		}
		else if (targetZombie.health > 500f || (bool)targetZombie.body.transform.Find("DogBody"))
		{
			targetZombie.Knockback(1000f, "knockback");
		}
		else if (base.transform.localScale.x > 0f)
		{
			targetZombie.Knockback(1000f, "fallDownBack");
		}
		else
		{
			targetZombie.Knockback(-1000f, "fallDownFront");
		}
		targetZombie = null;
	}

	public void AttackDone()
	{
		isAttacking = false;
		Follow(1f);
		actionCooldown = Random.Range(0.5f, actionInterval);
	}

	public void Follow(float speed)
	{
		SetMovePos(player.transform.position + new Vector3(Random.Range(-1f, 3f), Random.Range(-2f, 2f), 0f));
		move = speed;
		state = State.Follow;
	}

	public void PlayAudioOneShot(AudioClip audioClip)
	{
		audioSource.PlayOneShot(audioClip);
	}

	public void PlayAudio(AudioClip audioClip)
	{
		if (audioSource.enabled)
		{
			audioSource.clip = audioClip;
			audioSource.Play();
		}
	}

	public void Woof()
	{
		if (audioSource.enabled)
		{
			audioSource.PlayOneShot(woof[Random.Range(0, woof.Length)]);
		}
	}

	public void AttackSound()
	{
		audioSource.PlayOneShot(attackSounds[Random.Range(0, attackSounds.Length)]);
	}

	public void WoofAgain()
	{
		if (detectedZombies.Count > 0 && !((double)Random.value > 0.5))
		{
			animator.Play("Woof", 0, 0.65f);
		}
	}
}
